package cn.jslancer.spider;

import org.springframework.util.StringUtils;

import java.util.*;
/**
 * URL 管理器
 *
 * @author jslancer
 * @date 2017/11/5
 */
public class UrlManager {

    private Queue<String> urls = new LinkedList<>();

    public void addUrl(String url) {
        if (StringUtils.isEmpty(url)) {
            return;
        }
        if (!urls.contains(url)) {
            this.urls.offer(url);
        }
    }

    public void addUrls(List<String> urls) {
        if (urls == null || urls.size() == 0) {
            return;
        }
        //同步方法块，防止多线程并发
        synchronized (this) {
            urls.stream()
                    .filter(s -> !s.isEmpty() && !this.urls.contains(s))
                    .distinct()
                    .forEach(s -> this.addUrl(s));
        }
    }

    public String getUrl() {
        return this.urls.poll();
    }

    public boolean hasUrl() {
        return urls.size() != 0;
    }
}
